﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace LOGIN
{
    public partial class ManagerRole : Form
    {
        public ManagerRole()
        {
            InitializeComponent();
            this.dgv_ManagerRoleTable.AllowUserToAddRows = false;
        }

        private void ManagerRole_Load(object sender, EventArgs e)
        {
            SqlConnection sqlConnection = new SqlConnection();
            sqlConnection.ConnectionString =
                "Server=(local);Database=MyHospital;Integrated Security=sspi";
            SqlCommand sqlCommand = new SqlCommand();
            sqlCommand.Connection = sqlConnection;
            sqlCommand.CommandText = "SELECT c.RoleID,UserName,RoleName,GiveOutMedicine,MedicineManager,OrdersManager,SupplierManager,UserCenter,UserManager FROM manager AS c JOIN managerRole AS cs ON c.RoleID=cs.RoleID;";
            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();
            sqlDataAdapter.SelectCommand = sqlCommand;
            DataTable ManagerRoleTable = new DataTable();

            sqlConnection.Open();

            sqlDataAdapter.Fill(ManagerRoleTable);

            sqlConnection.Close();
            this.dgv_ManagerRoleTable.DataSource = ManagerRoleTable;
            dgv_ManagerRoleTable.Columns["RoleID"].ReadOnly = true;
            dgv_ManagerRoleTable.Columns["UserName"].ReadOnly = true;
            dgv_ManagerRoleTable.Columns["RoleName"].ReadOnly = true;
            dgv_ManagerRoleTable.Columns["RoleID"].HeaderText = "角色ID";
            dgv_ManagerRoleTable.Columns["UserName"].HeaderText = "用户名";
            dgv_ManagerRoleTable.Columns["RoleName"].HeaderText = "角色";
            dgv_ManagerRoleTable.Columns["GiveOutMedicine"].HeaderText = "退货管理";
            dgv_ManagerRoleTable.Columns["MedicineManager"].HeaderText = "药品管理";
            dgv_ManagerRoleTable.Columns["OrdersManager"].HeaderText = "订单管理";
            dgv_ManagerRoleTable.Columns["SupplierManager"].HeaderText = "供应商管理";
            dgv_ManagerRoleTable.Columns["UserCenter"].HeaderText = "信息维护管理";
            dgv_ManagerRoleTable.Columns["UserManager"].HeaderText = "权限管理";

            //this.dgv_ManagerRoleTable.Columns[this.dgv_ManagerRoleTable.Columns.Count - 1].AutoSizeMode =
            //    DataGridViewAutoSizeColumnMode.Fill;
        }

        private void btn_OK_Click(object sender, EventArgs e)
        {
            SqlConnection sqlConnection = new SqlConnection();
            sqlConnection.ConnectionString =
                "Server=(local);Database=MyHospital;Integrated Security=sspi";
           
            
            SqlCommand updateCommand = new SqlCommand();                                                    
            updateCommand.Connection = sqlConnection;                                              
            updateCommand.CommandText =                                                         
                "UPDATE managerRole"
                + " SET GiveOutMedicine=@GiveOutMedicine,MedicineManager=@MedicineManager,OrdersManager=@OrdersManager,SupplierManager=@SupplierManager,UserCenter=@UserCenter,UserManager=@UserManager"
                + " WHERE RoleID=@RoleID;";
            updateCommand.Parameters.Add("@GiveOutMedicine", SqlDbType.Bit, 0, "GiveOutMedicine");
            updateCommand.Parameters.Add("@MedicineManager", SqlDbType.Bit, 0, "MedicineManager");
            updateCommand.Parameters.Add("@OrdersManager", SqlDbType.Bit, 0, "OrdersManager");
            updateCommand.Parameters.Add("@SupplierManager", SqlDbType.Bit, 0, "SupplierManager");
            updateCommand.Parameters.Add("@UserCenter", SqlDbType.Bit, 0, "UserCenter");
            updateCommand.Parameters.Add("@UserManager", SqlDbType.Bit, 0, "UserManager");
            updateCommand.Parameters.Add("@RoleID", SqlDbType.VarChar, 0, "RoleID");
            
            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();
                                                          
            sqlDataAdapter.UpdateCommand = updateCommand;

            DataTable ManagerRoleTable1 = (DataTable)this.dgv_ManagerRoleTable.DataSource;                               
            sqlConnection.Open();
            int rowAffected = sqlDataAdapter.Update(ManagerRoleTable1);                                        
            sqlConnection.Close();                                                                          
            MessageBox.Show("更新" + rowAffected.ToString() + "行。");    
        }
    }
}
